//
//  CheckIphoneTools.m
//  ToolLibrary
//
//  Created by mac on 2018/9/25.
//  Copyright © 2018年 杨洋. All rights reserved.
//

#import "CheckIphoneTools.h"
#import <sys/sysctl.h>
#import <sys/utsname.h>

@implementation CheckIphoneTools
// 获取 device model/machine name 的方法一
/*
 iPhone 6s              iPhone8,1
 iPhone X               iPhone10,3 或  iPhone10,6
 iPhone XS              iPhone11,2
 iPhone XS Max          iPhone11,4 或 iPhone11,6
 iPhone XR              iPhone11,8
 
 */

+(iPhoneType)checkiPhoneType
{
    NSString *iPhoneType = @"";
#if TARGET_IPHONE_SIMULATOR
    // 获取模拟器所对应的 device model
    NSString *model = NSProcessInfo.processInfo.environment[@"SIMULATOR_MODEL_IDENTIFIER"];
    iPhoneType = model;
#else
    // 获取真机设备的 device model
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *model = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    iPhoneType = model;
#endif
    if ([iPhoneType isEqualToString:@"iPhone10,3"] || [iPhoneType isEqualToString:@"iPhone10,6"]) {
        return iPhoneType_X;
    }
    else if ([iPhoneType isEqualToString:@"iPhone11,2"])
    {
        return iPhoneType_XS;
    }
    else if ([iPhoneType isEqualToString:@"iPhone11,4"] || [iPhoneType isEqualToString:@"iPhone11,6"])
    {
        return iPhoneType_XS_Max;
    }
    else if ([iPhoneType isEqualToString:@"iPhone11,8"])
    {
        return iPhoneType_XR;
    }
    return -1;
}

+(NSString *)checkiPhoneTypeForStringInfo
{
    NSString *iPhoneType = @"";
#if TARGET_IPHONE_SIMULATOR
    // 获取模拟器所对应的 device model
    NSString *model = NSProcessInfo.processInfo.environment[@"SIMULATOR_MODEL_IDENTIFIER"];
    iPhoneType = model;
#else
    // 获取真机设备的 device model
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *model = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    iPhoneType = model;
#endif
    if ([iPhoneType isEqualToString:@"iPhone10,3"] || [iPhoneType isEqualToString:@"iPhone10,6"]) {
        return @"iPhone X";
    }
    else if ([iPhoneType isEqualToString:@"iPhone11,2"])
    {
        return @"iPhone XS";
    }
    else if ([iPhoneType isEqualToString:@"iPhone11,4"] || [iPhoneType isEqualToString:@"iPhone11,6"])
    {
        return @"iPhone XS Max";
    }
    else if ([iPhoneType isEqualToString:@"iPhone11,8"])
    {
        return @"iPhone XR";
    }
    return iPhoneType;
}

+ (NSString *)machineName1 {
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = (char *)malloc(size);
    if (machine == NULL) {
        return nil;
    }
    sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
    free(machine);
    return platform;
}

// 获取 device model/machine name 的方法二
+ (NSString *)machineName2 {
    struct utsname systemInfo;
    uname(&systemInfo);
    return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
}

+ (BOOL)isiPhoneX {
    static BOOL isiPhoneX = NO;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
#if TARGET_IPHONE_SIMULATOR
        // 获取模拟器所对应的 device model
        NSString *model = NSProcessInfo.processInfo.environment[@"SIMULATOR_MODEL_IDENTIFIER"];
#else
        // 获取真机设备的 device model
        struct utsname systemInfo;
        uname(&systemInfo);
        NSString *model = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
#endif
        // 判断 device model 是否为 "iPhone10,3" 和 "iPhone10,6" 或者以 "iPhone11," 开头
        // 如果是，就认为是 iPhone X
        isiPhoneX = [model isEqualToString:@"iPhone10,3"] || [model isEqualToString:@"iPhone10,6"] || [model hasPrefix:@"iPhone11,"];
    });
    
    return isiPhoneX;
}

@end
